<?php
error_reporting(E_ALL ^ E_NOTICE); 

class sysApps {
	
	/* LIVE SITE */
	/*private static $dbname = 'admin_mgmDB';
	private static $dbuser = 'admin_mgm';
	private static $dbpass = '{i12xs}';*/
	
	/* TEST SITE */
	private static $dbname = 'admin_mgm_testDB';
	private static $dbuser = 'admin_mgm';
	private static $dbpass = '{i12xs}';
	
	/* LOCAL SITE */
	/*private static $dbname = 'admin_mgm_testDB';
	private static $dbuser = 'root';
	private static $dbpass = '';*/
	
	public function DBconnect()	{
		mysql_connect('localhost',self::$dbuser,self::$dbpass);
		@mysql_select_db(self::$dbname) or die( "Unable to select database");
	}
	
	public function BASEurl()	{
		echo "http://".$_SERVER['HTTP_HOST'].'/'; //test site 
		//echo "http://".$_SERVER['HTTP_HOST'].'/subscribeTest/'; // local site
		//echo SRCWEB_BASE.DS;
	}
	
	public function getPage($page) {
		//$page = $_SESSION['page'];
		
		switch ($page) {
			case 'edit-profile':
				include 'apps/edit-profile.php';
			break;
			case 'tree':
				include 'apps/tree.php';
			break;
			case 'edit-profile':
				include 'apps/edit-profile.php';
			break;
			case 'change-password':
				include 'apps/change-password.php';
			break;
			case 'submission-list':
				include 'apps/submission-list.php';
			break;
			case 'key-tree':
				include 'apps/key-list.php';
			break;
			case 'income-tree':
				include 'apps/income-list.php';
			break;
			case 'payment-info':
				include 'apps/payment-info.php';
			break;
			case 'payment-update':
				include 'apps/payment-update.php';
			break;
			case 'success':
				include 'apps/success.php';
			break;
			case 'activate':
				include 'apps/activate.php';
			break;
			case 'activate-process':
				include 'apps/activate-process.php';
			break;
			case 'calculate-income':
				if($_SESSION['user_type'] == "Administrator")
				{
					include 'apps/calculateMultipleIncome.php';
				}
				else
				{
					include 'apps/calculateSingleIncome.php';
				}		
				
			break;
			case 'logout':
				header('Location: logout.php');
				//include 'apps/logout.php';
			break;
			
			default:
				include 'apps/home.php';
		}
	}
	

	
	public function userInfo($id, $info) {
		$sysApps = new sysApps();
		$sysApps->DBconnect();
		//$id = $_SERVER['user_MGM_id'];
		$sql = "SELECT * FROM mgm_users WHERE user_MGM_id = '".$id."' ";
		$query = mysql_query($sql);
		$row = mysql_fetch_array($query);
		return $row[$info];
		//return $info;
	}
	
	public function saveProfile() {
		$sysApps = new sysApps();
		$sysApps->DBconnect();
		$id = htmlentities($_POST['mgm_id']);
		$fullname = htmlentities($_POST['fullname']);
		$address1 = htmlentities($_POST['address1']);
		$address2 = htmlentities($_POST['address2']);
		$state = htmlentities($_POST['state']);
		$country = htmlentities($_POST['country']);
		$phone_no = htmlentities($_POST['phone_no']);
		$mobile_no = htmlentities($_POST['mobile_no']);
		
		//echo $id." + ".$fullname." + ".$address1." + ".$address2." + ".$state." + ".$country." + ".$phone_no." + ".$mobile_no;
		
		
		$sql = "UPDATE mgm_users SET fullname = '".$fullname."', address1 = '".$address1."', address2 = '".$address2."' ,state = '".$state."' , country = '".$country."', phone_no = '".$phone_no."', mobile_no = '".$mobile_no."' WHERE user_MGM_id = '".$id."'";
		$query = mysql_query($sql);
		
		if (!$query) {
			die(mysql_error());
		} else {
			echo '1';
		}
	}
	
	public function changePassword() {
		$sysApps = new sysApps();
		$sysApps->DBconnect();
		
		$id = htmlentities($_POST['id']);
		$new_pass = htmlentities($_POST['new_pass']);
		$old_pass = htmlentities($_POST['old_pass']);
		
		$sql = "SELECT password FROm mgm_users WHERE user_MGM_id = '".$id."' ";
		$query = mysql_query($sql);
		$row = mysql_fetch_array($query);
		if ($old_pass != $row['password']) {
			echo '2'; //wrong password
		} else {
			$update = "UPDATE mgm_users SET password='".$new_pass."' WHERE user_MGM_id = '".$id."'";
			$query = mysql_query($update);
			echo '1'; //success
		}
	}
	
	public function saveBankInfo() {
		$sysApps = new sysApps();
		$sysApps->DBconnect();
		$mgm_id = htmlentities($_POST['id']);
		$select_bank = htmlentities($_POST['select_bank']);
		$bank_accno = htmlentities($_POST['bank_accno']);
		
		//echo $id." + ".$select_bank." + ".$bank_accno;
		$sql = "UPDATE mgm_users SET bank_code = '".$select_bank."', bank_accNo = '".$bank_accno."' WHERE user_MGM_id = '".$mgm_id."' ";
		$query = mysql_query($sql);
		
		if (!$query) {
			die(mysql_error());
		} else {
			echo '1';
		}
	}
	
	public function checkBankInfo($id) {
		$sysApps = new sysApps();
		$sysApps->DBconnect();
		
		$sql = "SELECT bank_code, bank_accNo FROM mgm_users WHERE user_MGM_id = '".$id."' ";
		$query = mysql_query($sql);
		while ($row = mysql_fetch_array($query)) {
			$bank_code = $row['bank_code'];
			$bank_accNo = $row['bank_accNo'];
			
			if ($bank_code == '' || $bank_accNo == '') {
				return '1';
			}
		}
		
	}
	
	public function sendEmailActivate($id) {
		$sysApps = new sysApps();
		$sysApps->DBconnect();
		include 'apps/mailActivate.php';
	}
	
} // end class mgmApps

?>